package org.elsfs.cloud.common.mybatis.ext.db;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.elsfs.cloud.common.mybatis.ext.sql.support.executor.DeleteSqlExecutor;
import org.elsfs.cloud.common.mybatis.ext.sql.support.executor.InsertSqlExecutor;
import org.elsfs.cloud.common.mybatis.ext.sql.support.executor.QueryListSqlExecutor;
import org.elsfs.cloud.common.mybatis.ext.sql.support.executor.QueryOneSqlExecutor;
import org.elsfs.cloud.common.mybatis.ext.sql.support.executor.QueryPageSqlExecutor;
import org.elsfs.cloud.common.mybatis.ext.sql.support.executor.UnionSqlExecutor;
import org.elsfs.cloud.common.mybatis.ext.sql.support.executor.UpdateSqlExecutor;


/**
 * SQL执行器静态实现
 *
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class SqlExecutors {

    /**
     * SQL执行器工厂
     */
    private static SqlExecutorFactory sqlExecutorFactory;

    /**
     * 设置SQL执行器工厂
     *
     * @param sqlExecutorFactory SQL执行器工厂
     */
    static void setSqlExecutorFactory(SqlExecutorFactory sqlExecutorFactory) {
        SqlExecutors.sqlExecutorFactory = sqlExecutorFactory;
    }

    /**
     * 查询单条数据
     *
     * @param clazz 数据项类
     * @param <R>   结果类型
     * @return SQL执行器
     */
    public static <R> QueryOneSqlExecutor<R> queryOne(Class<R> clazz) {
        return sqlExecutorFactory.queryOne(clazz);
    }

    /**
     * 联合数据查询
     *
     * @param clazz 数据项类
     * @param <R>   结果类型
     * @return SQL执行器
     */
    public static <R> UnionSqlExecutor<R> unionQuery(Class<R> clazz) {
        return sqlExecutorFactory.unionQuery(clazz);
    }

    /**
     * 查询列表数据
     *
     * @param clazz 数据项类
     * @param <R>   结果类型
     * @return SQL执行器
     */
    public static <R> QueryListSqlExecutor<R> queryList(Class<R> clazz) {
        return sqlExecutorFactory.queryList(clazz);
    }

    /**
     * 查询分页数据
     *
     * @param pageable 分页对象
     * @param clazz    数据项类
     * @param <R>      结果类型
     * @return SQL执行器
     */
    public static <R> QueryPageSqlExecutor<R> queryPage(IPage<R> pageable, Class<R> clazz) {
        return sqlExecutorFactory.queryPage(pageable, clazz);
    }

    /**
     * 插入数据
     *
     * @return SQL执行器
     */
    public static InsertSqlExecutor insert() {
        return sqlExecutorFactory.insert();
    }

    /**
     * 更新数据
     *
     * @return SQL执行器
     */
    public static UpdateSqlExecutor update() {
        return sqlExecutorFactory.update();
    }

    /**
     * 删除数据
     *
     * @return SQL执行器
     */
    public static DeleteSqlExecutor delete() {
        return sqlExecutorFactory.delete();
    }
}
